/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package im.clusterhead;

import im.eventpackage.Event;
import im.general.AbstractListenBehaviour;
import im.general.Config;
import im.messages.ResourceMessage;
import im.node.MobileNodeInfo;
import im.resources.ResourceStatus;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.lang.acl.UnreadableException;
import jade.wrapper.ControllerException;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author brownstone
 */
public class UpdateStatusListenBehaviour extends AbstractListenBehaviour{

    ClusterHeadAgent clusterHeadAgent;

    public UpdateStatusListenBehaviour(ClusterHeadAgent clusterHeadAgent) {
        this.clusterHeadAgent = clusterHeadAgent;
        mt = MessageTemplate.MatchEncoding(Config.UPDATE_STATUS_NAME);
    }
    @Override
    protected void handleMessage(ACLMessage msg) {
        try {
            ResourceMessage resourceMessage = null;
            try {
                resourceMessage = (ResourceMessage) msg.getContentObject();
            } catch (UnreadableException ex) {
                Logger.getLogger(UpdateStatusListenBehaviour.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            String node = msg.getSender().getName();

            //mancava -> Dan
            //this.clusterHeadAgent.setNodeStatus(node,resourceMessage.getResourceStatus() );

            
            if (resourceMessage.getContainerID().equals(clusterHeadAgent.getContainerController().getContainerName()) ){
                handleMyStatusUpdate(resourceMessage.getResourceStatus());
            } else {
                handleStatusUpdate(node, resourceMessage.getResourceStatus());
            }
        } catch (ControllerException ex) {
            Logger.getLogger(UpdateStatusListenBehaviour.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void handleMyStatusUpdate(ResourceStatus resourceStatus) {
        if(resourceStatus.isLow()) {
            Enumeration names = clusterHeadAgent.getNodeNames();
            Enumeration infos = clusterHeadAgent.getNodeInfos();
            String name, maxName=null;
            MobileNodeInfo info, maxInfo=null;
            
            for(name=(String) names.nextElement(), info=(MobileNodeInfo) infos.nextElement();
            names.hasMoreElements()&&infos.hasMoreElements();
            name=(String) names.nextElement(),info=(MobileNodeInfo) infos.nextElement()){
                if(maxInfo == null || info.hasMoreResources(maxInfo)){
                    maxInfo = info;
                    maxName = name;
                }
            }
            
            clusterHeadAgent.moveAgent(maxName);
        }
    }

    private void handleStatusUpdate(String node, ResourceStatus resourceStatus) {
        //MobileNodeInfo info = clusterHeadAgent.getNodeInfo(node);
        //info.setResourceStatus(resourceStatus);
        //clusterHeadAgent.setNodeInfo(node, info);
        clusterHeadAgent.setResourceStatus(node, resourceStatus);
    }

}
